//
//  ModuleCell.swift
//  demo01
//
//  Created by babybus on 2025/6/27.
//
import Foundation
import UIKit
import SnapKit

class ModuleCell:UICollectionViewCell,UICollectionViewDelegate,
                 UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
    public var cellLabel:UILabel={
        let label = UILabel();
        label.textAlignment = .left
        label.backgroundColor  = .white
        label.textColor = .black
        return label
    }()
    public var collectionView:UICollectionView!
    private let containerStack:UIStackView = {
        let stack = UIStackView();
        stack.axis = .vertical
        stack.alignment = .center
        stack.distribution = .fill	
        return stack
    }()
    public var items:[ModuleInfo] = []
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:NSCoder) -- fatalError")
    }
    func configure(item:[ModuleInfo]){
        self.items = item
    }
    
    
    func setupUI(){
        print("ModuleCell -- setupUI")
        
        let layout = UICollectionViewFlowLayout();
        layout.scrollDirection = .vertical;
        collectionView = UICollectionView(frame: self.bounds, collectionViewLayout: layout)
        collectionView?.delegate = self
        collectionView?.dataSource = self
        collectionView?.register(ModuleItemCell.self, forCellWithReuseIdentifier: "ModuleItemCell")
        collectionView?.register(MyHeaderView.self,
                              forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader,
                              withReuseIdentifier: "MyHeaderView")
        collectionView.layer.cornerRadius = 20
        collectionView.clipsToBounds = true
        
        containerStack.addArrangedSubview(collectionView!)
        contentView.addSubview(containerStack)
        containerStack.snp.makeConstraints{ make in
            make.left.right.equalToSuperview().inset(8)
            make.top.bottom.equalToSuperview().inset(8)
        }
        collectionView.snp.makeConstraints{make in
            make.top.equalToSuperview().inset(0)
            make.width.equalToSuperview()
        }
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return items.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ModuleItemCell", for: indexPath)as! ModuleItemCell
        let item = items[indexPath.row]
        cell.configure(with: item)
        return cell
    }

    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        if kind == UICollectionView.elementKindSectionHeader{
            
            let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "MyHeaderView", for: indexPath)as! MyHeaderView
            header.title = cellLabel.text
            return header
        }
        return UICollectionReusableView()
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let width = (collectionView.bounds.width)/5
        let imageHeight = 16.0;
        let textHeight:CGFloat = 40;
        return CGSize(width: width, height: imageHeight + textHeight + 24);
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: collectionView.bounds.width, height: 30)
    }
}
